<template>
	<div class="monitor-5">
		<div>
			<el-date-picker @change='dateChange' v-model="createTime" type="date" :placeholder="$t('route.Select_date')" />
			<label>{{ $t('route.Sewing_power') }}</label>
			<el-input-number v-model="sewPower" />
			<el-button @click='dataTypeClick'>{{ $t('route.switch') }}</el-button>

			<div class="div-handle-right">
				<el-button @click='refreshClick'>{{ isRefresh ? $t('route.Stop_Refresh') : $t('route.auto_refresh')
				}}</el-button>
				<el-pagination :page-sizes="[120, 300, 600, 1200]" @size-change="handleSizeChange" style="float: right;"
					background @current-change="handlePageChange" :current-page="query.pageNum" :page-size="query.pageSize"
					layout="total, sizes,prev, pager, next" :total="query.total">
				</el-pagination>
			</div>
		</div>

		<div>
			<div v-if="chartShow" class="chart" id="echart-1"></div>
			<div v-if="chartShow" class="chart" id="echart-2"></div>
		</div>

		<div>
			<div>
				<div class="title">{{$t('route.jockey')}}</div>
				<div class="content ">
					<div>{{ stdSewInfo.nickname }}</div>
					<div>{{ device.kind }}</div>
					<div>{{ device.noseCode }}</div>
				</div>
			</div>
			<div>
				<div class="title">{{$t('route.Time6')}}<label style="color: #f00;"></label></div>
				<div class="content">
					<div v-for="item in stdSewInfo.craftList">
						<label :style="{ color: item.craftCode == selCraft.craftCode ? '#f60' : '#222' }"
							class="bottom-line">
							{{ item.mono }}
						</label>
					</div>
				</div>
			</div>
			<div>
				<div class="title">{{$t('route.working_procedure')}}<label style="color: #f00;">{{$t('route.process1')}}</label></div>
				<div class="content">
					<div v-for="item in stdSewInfo.craftList">
						<label @click="craftClick(item)"
							:style="{ color: item.craftCode == selCraft.craftCode ? '#f60' : '#222' }" class="bottom-line">
							{{ item.craftName }}
						</label>
					</div>
				</div>
			</div>
			<div>
				<div class="title">{{$t('route.Time8')}}</div>
				<div class="content" style="color: #f60;">{{$t('route.Value1')}}{{ avgStdSew }}{{$t('route.second')}} <label class="btn-click" @click="saveStdSew"
						style="margin-left:10px;color:#0A8">{{$t('route.preservation')}}</label>
				</div>
				<div class="content">
					<label style="text-align: left;margin:3px 0px;display: inline-block;min-width: 160px;max-width: 170px;"
						v-for="(value, index) in durList ">{{$t('route.Value2')}}{{ index + 1
						}}{{$t('route.Value3')}}{{ value }}{{$t('route.second')}}</label>
				</div>
			</div>
			<div>
				<div class="title">{{$t('route.Real_time_work')}}<label @click="stdsewClick" style="margin-left:10px;color:#0A8"
						class="btn-click">{{$t('route.preservation')}}</label></div>
				<div class="content stdsew-text">{{ stdSew }}{{$t('route.second')}}</div>
			</div>

			<div>
				<div class="title">{{$t('route.Time9')}}</div>
				<div class="content">
					<div v-for="item in userStdSewList" v-show="item.userId != stdSewInfo.userId"
						class="flex-row  flex-space-between">
						<div>{{ item.nickname }}</div>
						<div>{{ item.sewDur }}{{$t('route.second')}}</div>
					</div>
				</div>
			</div>

		</div>

	</div>
</template>


<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import * as echarts from 'echarts';
import EchartsUtils from '../../js/common/echarts-utils.js'
import {
	markRaw
} from 'vue'
export default {
	data() {
		return {
			stdSewInfo: null,
			device: null,
			createTime: new Date(),
			isRefresh: false,
			mTimer: null,
			mChart1: null,
			mChart2: null,
			chartShow: false,
			stdSew: 0,
			sewPower: 30,
			isPower: true, //true:功率显示  false:缝纫模式显示
			yData1: [],
			xData1: [],
			yData2: [],
			xData2: [],
			data1: [],
			data2: [],
			option1: null,
			option2: null,
			query: {
				pageNum: 1,
				pageSize: 300,
				total: 0
			},
			durList: ['---', '---', '---', '---', '---', '---', '---', '---', '---', '---'],
			saveIndex: 0,
			avgStdSew: '---',
			selCraft: null,
			userStdSewList: []
		};
	},
	methods: {
		craftClick(item) {
			this.selCraft = item;
			this.durList = ['---', '---', '---', '---', '---', '---', '---', '---', '---', '---'];
			this.avgStdSew = '---';
			this.stdSew = 0;
			this.getStdSewList();
		},
		saveStdSew() {
			if (this.avgStdSew == '---') {
				this.$message.error(this.$t('route.Value4'))
				return;
			}
			var cnt = 0;
			for (var item of this.durList) {
				if (item != '---') {
					cnt++;
				}
			}
			http.post(api.ADD_STD_SEW, {
				adminId: this.$store.state.user.id,
				deviceId: this.device.id,
				userId: this.stdSewInfo.userId,
				mono: this.selCraft.mono,
				craftCode: this.selCraft.craftCode,
				craftName: this.selCraft.craftName,
				sewDur: this.avgStdSew,
				cnt: cnt
			}, data => {
				this.$message.success(this.$t('route.Saved_successfully'));
			});
		},
		stdsewClick() {
			if (this.stdSew == 0) {
				this.$message.error(this.$t('route.Value5'))
				return;
			}
			this.durList[this.saveIndex % 10] = this.stdSew;
			this.saveIndex++;
			var allStdSew = 0;
			var count = 0;
			for (var item of this.durList) {
				if (item != '---') {
					count++;
					allStdSew += item;
				}
			}
			if (count != 0) {
				this.avgStdSew = (allStdSew / count).toFixed(2);
			}


		},
		dataRefresh() {
			this.mChart1.clear();
			this.mChart2.clear();
			if (this.isPower) {
				this.yData1 = [];
				this.yData1 = [...this.data1];
				this.option1 = EchartsUtils.initLineChartOption(this.$t('route.Sewing_curve'), this.xData1, this.yData1);
				this.yData2 = [];
				this.yData2 = [...this.data2];
				this.option2 = EchartsUtils.initLineChartOption(this.$t('route.Sewing_curve'), this.xData2, this.yData2);
			} else {
				this.yData1 = [];
				for (var item of this.data1) {
					this.yData1.push(item < this.sewPower ? 0 : 1);
				}
				this.option1 = EchartsUtils.initStepLineChartOption(this.$t('route.Sewing_curve'), this.xData1, this.yData1);
				this.yData2 = [];
				for (var item of this.data2) {
					this.yData2.push(item < this.sewPower ? 0 : 1);
				}
				this.option2 = EchartsUtils.initStepLineChartOption(this.$t('route.Sewing_curve'), this.xData2, this.yData2);

			}
			this.mChart1.setOption(this.option1);
			this.mChart2.setOption(this.option2);
		},

		dataTypeClick() {
			this.isPower = !this.isPower;
			this.dataRefresh();
		},

		getPowerStepReport() {
			this.xData1 = [];
			this.yData1 = [];
			this.data1 = [];
			this.xData2 = [];
			this.yData2 = [];
			this.data2 = [];
			http.post(api.GET_POWER_STEP_REPORT, {
				deviceId: this.device.id,
				createTime: this.createTime.format('yyyy-MM-dd'),
				pageNum: this.query.pageNum,
				pageSize: this.query.pageSize / 30,
			}, data => {
				this.query.total = data.data.total * 30;
				for (var i = 0; i < data.data.dataList.length; i++) {
					if (i <= data.data.dataList.length / 2) {
						this.xData1.push(i + 's');
						this.yData1.push(data.data.dataList[i]);
						this.data1.push(data.data.dataList[i]);
					} else {
						this.xData2.push(i + 's');
						this.yData2.push(data.data.dataList[i]);
						this.data2.push(data.data.dataList[i]);
					}

				}
				this.initEchart1();
				this.initEchart2();
			});
		},
		getStdSewList() {
			http.post(api.GET_STD_SEW_LIST2, {
				adminId: this.$store.state.user.id,
				mono: this.selCraft.mono,
				craftCode: this.selCraft.craftCode,
			}, data => {
				this.userStdSewList = data.data;
			});

		},



		handlePageChange(pageNum) {
			this.query.pageNum = pageNum;
			this.getPowerStepReport();
		},
		handleSizeChange(val) {
			this.query.pageSize = val;
			this.getPowerStepReport();
		},
		dateChange() {
			this.getPowerStepReport();
		},
		refreshMarkLine1(xIndex, sliderIndex) {
			this.option1.series[0].markLine.data[sliderIndex].xAxis = xIndex;
			this.mChart1.setOption(this.option1);
			var index0 = this.option1.series[0].markLine.data[0].xAxis;
			var index1 = this.option1.series[0].markLine.data[1].xAxis;
			this.stdSew = Math.abs(index0 - index1);
		},

		refreshMarkLine2(xIndex, sliderIndex) {
			this.option2.series[0].markLine.data[sliderIndex].xAxis = xIndex;
			this.mChart2.setOption(this.option2);
			var index0 = this.option2.series[0].markLine.data[0].xAxis;
			var index1 = this.option2.series[0].markLine.data[1].xAxis;
			this.stdSew = Math.abs(index0 - index1);
		},


		initEchart1() {
			this.$nextTick(() => {
				if (this.mChart1 == null) {
					this.mChart1 = markRaw(echarts.init(document.getElementById("echart-1")));
					var sliderIndex = 0;
					this.mChart1.getZr().on('click', params => {
						let point = [params.offsetX, params.offsetY];
						if (this.mChart1.containPixel('grid', point)) {
							let xIndex = this.mChart1.convertFromPixel({
								seriesIndex: 0
							},
								point)[0];
							var index0 = this.option1.series[0].markLine.data[0].xAxis;
							var index1 = this.option1.series[0].markLine.data[1].xAxis;
							console.log('index0=====' + index0)
							console.log('index1=====' + index1);
							console.log('xIndex=====' + xIndex);
							if (index0 >= xIndex - 1 && index0 <= xIndex + 1) {
								sliderIndex = 0;
								console.log('===sliderIndex======' + sliderIndex)
							} else if (index1 >= xIndex - 1 && index1 <= xIndex + 1) {
								sliderIndex = 1;
								console.log('===sliderIndex======' + sliderIndex)
							}
							this.refreshMarkLine1(xIndex, sliderIndex);

						}
					});
				}
				this.dataRefresh();

			})
		},
		initEchart2() {
			this.$nextTick(() => {
				if (this.mChart2 == null) {
					this.mChart2 = markRaw(echarts.init(document.getElementById("echart-2")));
					var sliderIndex = 0;
					this.mChart2.getZr().on('click', params => {
						let point = [params.offsetX, params.offsetY];
						if (this.mChart2.containPixel('grid', point)) {
							// convertFromPixel转换像素坐标值到逻辑坐标系上的点
							let xIndex = this.mChart2.convertFromPixel({
								seriesIndex: 0
							},
								point)[0];
							var index0 = this.option2.series[0].markLine.data[0].xAxis;
							var index1 = this.option2.series[0].markLine.data[1].xAxis;
							if (index0 >= xIndex - 1 && index0 <= xIndex + 1) {
								sliderIndex = 0;
								console.log('===sliderIndex======' + sliderIndex)
							} else if (index1 >= xIndex - 1 && index1 <= xIndex + 1) {
								sliderIndex = 1;
								console.log('===sliderIndex======' + sliderIndex)
							}
							this.refreshMarkLine2(xIndex, sliderIndex);

						}
					});
				}
				this.dataRefresh();

			})
		},
		refreshClick() {
			this.isRefresh = !this.isRefresh;
		}
	},
	created() {
		this.chartShow = true;
		this.device = JSON.parse(CommonUtils.getParam(this, 'device', 'setDevice'));
		this.stdSewInfo = JSON.parse(CommonUtils.getParam(this, 'stdSewInfo', 'setStdSewInfo'));
		console.log(JSON.stringify(this.stdSewInfo))
		if (this.stdSewInfo.craftList.length > 0) {
			this.selCraft = this.stdSewInfo.craftList[0];
			this.getStdSewList();
		}
		this.getPowerStepReport();
		this.mTimer = setInterval(() => {
			if (this.isRefresh) this.getPowerStepReport();
		}, 15000)
	},
	unmounted() {
		clearInterval(this.mTimer);
		this.chartShow = false;
	},
};
</script>

<style lang="scss" scoped>
.monitor-5 {
	color: #444444;
	font-size: 18px;
	padding-right: 20px;
	height: calc(100vh - 100px);
	// overflow: scroll;
	scrollbar-width: none;
	/* firefox */
	-ms-overflow-style: none;
	/* IE 10+ */
	overflow-x: hidden;
	overflow-y: auto;

	>div:nth-of-type(1) {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-top: 10px;
		background-color: #ffffff;
		padding: 10px 20px;
		border-radius: 5px;

		>label {
			margin-left: 20px;
		}

		.div-handle-right {
			flex: 1;
			flex-direction: row-reverse;
			text-align: right;
			display: flex;
			align-items: center;

			>input:nth-of-type(1) {
				border: 1px solid #eeeeee;
				font-size: 15px;
				width: 150px;
				padding: 5px 8px;
				outline: none;
			}

			>button {
				color: #A866FF;
				margin-left: 10px;
				padding: 4px 20px;
			}
		}
	}

	>div:nth-of-type(2) {
		background-color: #ffffff;
		padding: 20px;
		border-radius: 5px;
		margin-top: 10px;

		.chart {
			height: 200px;
			width: 100%;

		}
	}

	>div:nth-of-type(3) {
		background-color: #ffffff;
		padding: 10px 20px;
		border-radius: 5px;
		margin-top: 10px;
		display: flex;
		// >div:nth-of-type(1){
		// 	background-color: #A866FF;
		// }

		>div {
			flex: 1;

			.title {
				padding: 10px 0px;
				color: #222;
				text-align: center;
				font-size: 20px;
				border-bottom: 1px solid #eeeeee;

				>button {
					margin: 0px;
					padding: 0px;
					font-size: 20px;
					margin-left: 10px;
					margin-top: -12px;
				}
			}

			.content {
				text-align: center;
				font-size: 17px;

				>div {
					margin: 5px 0px;
				}

				>button {
					margin: 0px;
					padding: 0px;
					font-size: 17px;
					margin-left: 10px;
				}
			}

			.stdsew-text {
				font-size: 60px;
				text-align: center;
				font-weight: 800;
			}


		}

		>div:nth-of-type(1),
		>div:nth-of-type(2) {
			max-width: 150px;
			min-width: 150px;
		}

		>div:nth-of-type(4) {
			max-width: 480px;
			min-width: 480px;
		}

		>div:nth-of-type(6) {
			max-width: 150px;
			min-width: 150px;
		}
	}

}
</style>
